home *** CD-ROM | disk | FTP | other *** search
/ Tricks of the Mac Game Programming Gurus / TricksOfTheMacGameProgrammingGurus.iso / More Source / C⁄C++ / Swar / util.c < prev   
Text File  |  1994-08-21  |  6KB  |  279 lines

  1.  
  2. #include "swar.h"
  3.  
  4. extern RGBColor    myWhite, myBlack, myRed, myBlue, myYellow, myGreen, myOrange, myGray, myDkBlue;
  5.  
  6. #define COMMAND_KEY            0x37        /* 'CLOVER' */
  7. #define SHIFT_KEY                0x38        /* 'SHIFT' */
  8. #define OPTION_KEY            0x3A        /* 'OPTION' */
  9. #define CONTROL_KEY            0x3B        /* 'CONTROL' */
  10. #define ESCAPE_KEY            0x35        /* 'ESC' */
  11. #define QUIT_KEY                0x0C        /* 'Q' */
  12. #define NEW_KEY                0x2D        /* 'N' */
  13. #define SPEEDUP_KEY            0x18        /* '+' */
  14. #define SLOWDOWN_KEY        0x1B        /* '-' */
  15. #define PAUSE_KEY            0x23        /* 'P' */
  16. #define INFO_KEY                0x22        /* 'I' */
  17. #define SOUND_KEY            0x01        /* 'S' */
  18. #define SHOT_KEY                0x3B        /* 'CONTROL' */
  19. #define UP_ARROW                0x7E        /* 'UP ARROW' */
  20. #define DOWN_ARROW            0x7D        /* 'DOWN ARROW' */
  21. #define LEFT_ARROW            0x7B        /* 'LEFT ARROW' */
  22. #define RIGHT_ARROW            0x7C        /* 'RIGHT ARROW' */
  23. #define THRUST_KEY            0x7E        /* 'UP ARROW' */
  24. #define DOWN_KEY                0x28        /* 'K */
  25. #define CW_KEY                0x7C        /* 'RIGHT ARROW' */
  26. #define CCW_KEY                0x7B        /* 'LEFT ARROW' */
  27. #define SPACE_BAR            0x31        /* ' ' */
  28. #define F1_KEY                    0x7A        /* F1 */
  29. #define F2_KEY                    0x78        /* F2 */
  30. #define F3_KEY                    0x63        /* F3 */
  31. #define F4_KEY                    0x76        /* F4 */
  32.  
  33. int
  34. AbsVal(arg)
  35.     int    arg;
  36. {
  37.     return(((arg < 0) ? 0 - arg: arg));
  38.     
  39. } /* AbsVal() */
  40.  
  41. pascal char
  42. isPressed(unsigned short keyID)
  43. {
  44.     KeyMap keys;
  45.     char *kp;
  46.     
  47.     GetKeys(keys);
  48.     kp = (char *) keys;
  49.     return(((kp[(keyID>>3)] >> (keyID & 7)) & 1));
  50.     
  51. } /* isPressed() */
  52.  
  53. int
  54. RngRnd(short min, short max)
  55. {
  56.     unsigned    rnd;
  57.     long        range, t;
  58.     
  59.     rnd = Random();
  60.     range = max - min;
  61.     t = (rnd * range) / 65536;
  62.     return(t + min);
  63.     
  64. } /* RngRnd() */
  65.  
  66. CheckKeyboard()
  67. {
  68.     extern    Boolean            gSoundOn;
  69.     extern SHIPREC            gShipRecs[MAX_PLAYERS];
  70.     static Boolean            optionStillPressed;
  71.     static short            barStillPressed;
  72.     static short            CWStillPressed;
  73.     static short            CCWStillPressed;
  74.     static short            thStillPressed;
  75.     extern Handle            gShotSoundH;
  76.  
  77. #define SHOT_SPEED    7
  78.  
  79.     if (gShipRecs[0].isAlive && isPressed(SHOT_KEY)) {
  80.         if (!barStillPressed) {
  81.             barStillPressed = 3;
  82.             switch (gShipRecs[0].dir) {
  83.                 case 0: CreateShot(gShipRecs[0].color, gShipRecs[0].where.h + 7,
  84.                                     gShipRecs[0].where.v - 2,
  85.                                     0,
  86.                                     gShipRecs[0].vel.v - SHOT_SPEED);
  87.                     break;
  88.                 case 1: CreateShot(gShipRecs[0].color, gShipRecs[0].where.h + 17,
  89.                                     gShipRecs[0].where.v - 2,
  90.                                     gShipRecs[0].vel.h + SHOT_SPEED,
  91.                                     gShipRecs[0].vel.v - SHOT_SPEED);
  92.                     break;
  93.                 case 2: CreateShot(gShipRecs[0].color, gShipRecs[0].where.h + 17,
  94.                                     gShipRecs[0].where.v + 7,
  95.                                     gShipRecs[0].vel.h + SHOT_SPEED,
  96.                                     0);
  97.                     break;
  98.                 case 3: CreateShot(gShipRecs[0].color, gShipRecs[0].where.h + 17,
  99.                                     gShipRecs[0].where.v + 17,
  100.                                     gShipRecs[0].vel.h + SHOT_SPEED,
  101.                                     gShipRecs[0].vel.v + SHOT_SPEED);
  102.                     break;
  103.                 case 4: CreateShot(gShipRecs[0].color, gShipRecs[0].where.h + 7,
  104.                                     gShipRecs[0].where.v + 17,
  105.                                     0,
  106.                                     gShipRecs[0].vel.v + SHOT_SPEED);
  107.                     break;
  108.                 case 5: CreateShot(gShipRecs[0].color, gShipRecs[0].where.h - 2,
  109.                                     gShipRecs[0].where.v + 17,
  110.                                     gShipRecs[0].vel.h - SHOT_SPEED,
  111.                                     gShipRecs[0].vel.v + SHOT_SPEED);
  112.                     break;
  113.                 case 6: CreateShot(gShipRecs[0].color, gShipRecs[0].where.h - 2,
  114.                                     gShipRecs[0].where.v + 7,
  115.                                     gShipRecs[0].vel.h - SHOT_SPEED,
  116.                                     0);
  117.                     break;
  118.                 case 7: CreateShot(gShipRecs[0].color, gShipRecs[0].where.h - 2,
  119.                                     gShipRecs[0].where.v - 2,
  120.                                     gShipRecs[0].vel.h - SHOT_SPEED,
  121.                                     gShipRecs[0].vel.v - SHOT_SPEED);
  122.                     break;
  123.             } /* switch */
  124.             ASndPlay(gShotSoundH);
  125.         } /* if */
  126.     } /* if */
  127.  
  128.         if (barStillPressed)
  129.             barStillPressed--;
  130.     
  131.     gShipRecs[0].isAccel = FALSE;
  132.  
  133.     if (gShipRecs[0].isAlive && isPressed(THRUST_KEY)) {
  134.         if (!thStillPressed) {
  135.             thStillPressed = 5;
  136.             gShipRecs[0].isAccel = TRUE;
  137.         } /* if */
  138.     } /* if */
  139.  
  140.         if (thStillPressed)
  141.             thStillPressed--;
  142.  
  143.             
  144.     if (gShipRecs[0].isAlive && isPressed(CW_KEY)) {
  145.         if (!CWStillPressed) {
  146.             CWStillPressed = 5;
  147.             gShipRecs[0].dir++;
  148.         } /* if */
  149.     } /* if */
  150.  
  151.         if (CWStillPressed)
  152.             CWStillPressed--;
  153.  
  154.     if (gShipRecs[0].isAlive && isPressed(CCW_KEY)) {
  155.         if (!CCWStillPressed) {
  156.             CCWStillPressed = 5;
  157.             gShipRecs[0].dir--;
  158.         } /* if */
  159.     } /* if */
  160.  
  161.         if (CCWStillPressed)
  162.             CCWStillPressed--;
  163.  
  164.     if (gShipRecs[0].dir < 0)
  165.         gShipRecs[0].dir = 7;
  166.     if (gShipRecs[0].dir > 7)
  167.         gShipRecs[0].dir = 0;
  168.         
  169.     if (isPressed(COMMAND_KEY) && isPressed(NEW_KEY)) {
  170.         InitGame();
  171.         FlushEvents(everyEvent, 0);
  172.     } /* if */
  173.     
  174. //    if (isPressed(COMMAND_KEY) && isPressed(PAUSE_KEY)) {
  175. //        while (!Button());
  176. //        FlushEvents(everyEvent, 0);
  177. //    } /* if */
  178.     
  179. //    if (isPressed(COMMAND_KEY) && isPressed(SOUND_KEY)) {
  180. //        if (gSoundOn)
  181. //            gSoundOn = FALSE;
  182. //        else
  183. //            gSoundOn = TRUE;
  184. //        SetChecks();
  185. //        FlushEvents(everyEvent, 0);
  186. //    } /* if */
  187.     
  188. } /* CheckKeyboard() */
  189.  
  190. SetChecks()
  191. {
  192.     extern MenuHandle        gFileMenu;
  193.     extern Boolean            gSoundOn, gPauseOn;
  194.  
  195.     if (gPauseOn)
  196.         SetItemMark (gFileMenu, 2, '\022');
  197.     else
  198.         SetItemMark (gFileMenu, 2, noMark);
  199.  
  200.     if (gSoundOn)
  201.         SetItemMark (gFileMenu, 3, '\022');
  202.     else
  203.         SetItemMark (gFileMenu, 3, noMark);
  204.  
  205. } /* SetChecks() */
  206.  
  207. LoadSound(soundH, soundID, soundName)
  208.     Handle        *soundH;
  209.     short        soundID;
  210.     Str255        soundName;
  211. {
  212.     if ((*soundH = GetResource('snd ', soundID)) == 0L) {
  213.         ExitAppl();
  214.     } /* if */
  215.     MoveHHi(*soundH);
  216.     HLock(*soundH);
  217.     HNoPurge(*soundH);
  218.     
  219. } /* LoadSound() */
  220.  
  221. UnloadSound(soundH)
  222.     Handle    soundH;
  223. {
  224.     HUnlock(soundH);
  225.     HPurge(soundH);
  226.     ReleaseResource(soundH);
  227.  
  228. } /* UnloadSound() */
  229.  
  230. ExitAppl()
  231. {
  232.     extern    CWindowPtr    gPictureWindow;
  233.     extern    CGrafPort        *gOSPtr, *gScrapPtr;
  234.     
  235.     if (gPictureWindow)
  236.         DisposeWindow((WindowPtr)gPictureWindow);
  237.     if (gOSPtr) {
  238.         CloseCPort(gOSPtr);
  239.         DisposePtr((Ptr)gOSPtr);
  240.     } /* if */
  241.     if (gScrapPtr) {
  242.         CloseCPort(gScrapPtr);
  243.         DisposePtr((Ptr)gScrapPtr);
  244.     } /* if */
  245.     ShowCursor();
  246.     FlushEvents(everyEvent, 0);
  247.     UnloadSounds();
  248.     ExitToShell();
  249.     
  250. } /* ExitAppl() */
  251.  
  252. ErasePort(whichPort, whatRect)
  253.     CGrafPort        *whichPort;
  254.     Rect            whatRect;
  255. {
  256.     GrafPtr        savePort;
  257.     
  258.     GetPort(&savePort);
  259.     SetPort((GrafPtr)whichPort);
  260.     EraseRect (&whatRect);    
  261.     SetPort(savePort);
  262.     
  263. } /* ErasePort() */
  264.  
  265. Boolean
  266. ColorsEqual(rgbc1, rgbc2)
  267.     RGBColor    rgbc1, rgbc2;
  268. {
  269.     if (rgbc1.red != rgbc2.red)
  270.         return(FALSE);
  271.     if (rgbc1.green != rgbc2.green)
  272.         return(FALSE);
  273.     if (rgbc1.blue != rgbc2.blue)
  274.         return(FALSE);
  275.         
  276.     return(TRUE);
  277.         
  278. } /* ColorsEqual() */
  279.